# -*- coding: utf-8 -*-

from django.db import connection,transaction
from models import ModelTypes
from django.http import HttpResponse
from collections import OrderedDict
from django.utils import simplejson as json


def getItemList(request):
    cursor = connection.cursor()
    sql = """ select a.deployId,b.value from mvc_item a,mvc_language b where b.`id` = a.`name` and b.`type` = 6 and b.`key`='name' order by b.value """
    cursor.execute(sql)
    result = cursor.fetchall()
    resultList = []
    resultList.append([-999,"未选择"])
    for element in result : 
        resultList.append([element[0],element[1]])
    return HttpResponse(json.dumps({'code':1,'result':json.dumps(resultList)}), mimetype='application/json')

def getItem(request):
    index = int(request.GET.get('index'))
    elementId = int(request.GET.get('elementId'))
    elementName = getItemName(elementId)
    return HttpResponse(json.dumps({'code':1,'result':[index,elementName]}), mimetype='application/json')

def getItemName(elementId):
    cursor = connection.cursor()
    sql = """ select a.deployId,b.value from mvc_item a,mvc_language b where b.`id` = a.`name` and b.`type` = 6 and b.`key`='name' and a.`deployId`=%s """
    cursor.execute(sql,(elementId,))
    result = cursor.fetchall()
    resultName = "未选择"
    for element in result : 
        resultName = element[1]
    return resultName
    

def getCardList(request):
    cursor = connection.cursor()
    sql = """ select a.deployId,b.value from mvc_card a,mvc_language b where b.`id` = a.`name` and b.`type` = 13 and b.`key`='name' order by b.value """
    cursor.execute(sql)
    result = cursor.fetchall()
    resultList = []
    resultList.append([-999,"未选择"])
    for element in result : 
        resultList.append([element[0],element[1]])
    return HttpResponse(json.dumps({'code':1,'result':json.dumps(resultList)}), mimetype='application/json')

def getCard(request):
    index = int(request.GET.get('index'))
    elementId = int(request.GET.get('elementId'))
    elementName = getCardName(elementId)
    return HttpResponse(json.dumps({'code':1,'result':[index,elementName]}), mimetype='application/json')

def getCardName(elementId):
    cursor = connection.cursor()
    sql = """ select a.deployId,b.value from mvc_card a,mvc_language b where b.`id` = a.`name` and b.`type` = 13 and b.`key`='name' and a.`deployId`=%s """
    cursor.execute(sql,(elementId,))
    result = cursor.fetchall()
    resultName = "未选择"
    for element in result : 
        resultName = element[1]
    return resultName
    

def getMapList(request):
    cursor = connection.cursor()
    sql = """ select a.deployId,b.value from mvc_map a,mvc_language b where b.`id` = a.`name` and b.`type` = 1 and b.`key`='name' order by b.value """
    cursor.execute(sql)
    result = cursor.fetchall()
    resultList = []
    resultList.append([-999,"未选择"])
    for element in result : 
        resultList.append([element[0],element[1]])
    return HttpResponse(json.dumps({'code':1,'result':json.dumps(resultList)}), mimetype='application/json')

def getMap(request):
    index = int(request.GET.get('index'))
    elementId = int(request.GET.get('elementId'))
    elementName = getMapName(elementId)
    return HttpResponse(json.dumps({'code':1,'result':[index,elementName]}), mimetype='application/json')

def getMapName(elementId):
    cursor = connection.cursor()
    sql = """ select a.deployId,b.value from mvc_map a,mvc_language b where b.`id` = a.`name` and b.`type` = 1 and b.`key`='name' and a.`deployId`=%s """
    cursor.execute(sql,(elementId,))
    result = cursor.fetchall()
    resultName = "未选择"
    for element in result : 
        resultName = element[1]
    return resultName
    

def getCampaignList(request):
    cursor = connection.cursor()
    sql = """ select a.deployId,b.value from mvc_campaign a,mvc_language b where b.`id` = a.`name` and b.`type` = 2 and b.`key`='name' order by b.value """
    cursor.execute(sql)
    result = cursor.fetchall()
    resultList = []
    resultList.append([-999,"未选择"])
    for element in result : 
        resultList.append([element[0],element[1]])
    return HttpResponse(json.dumps({'code':1,'result':json.dumps(resultList)}), mimetype='application/json')

def getCampaign(request):
    index = int(request.GET.get('index'))
    elementId = int(request.GET.get('elementId'))
    elementName = getCampaignName(elementId)
    return HttpResponse(json.dumps({'code':1,'result':[index,elementName]}), mimetype='application/json')

def getCampaignName(elementId):
    cursor = connection.cursor()
    sql = """ select a.deployId,b.value from mvc_campaign a,mvc_language b where b.`id` = a.`name` and b.`type` = 2 and b.`key`='name' and a.`deployId`=%s """
    cursor.execute(sql,(elementId,))
    result = cursor.fetchall()
    resultName = "未选择"
    for element in result : 
        resultName = element[1]
    return resultName
    

def getSkillList(request):
    cursor = connection.cursor()
    sql = """ select a.deployId,b.value from mvc_skill a,mvc_language b where b.`id` = a.`name` and b.`type` = 7 and b.`key`='name' order by b.value """
    cursor.execute(sql)
    result = cursor.fetchall()
    resultList = []
    resultList.append([-999,"未选择"])
    for element in result : 
        resultList.append([element[0],element[1]])
    return HttpResponse(json.dumps({'code':1,'result':json.dumps(resultList)}), mimetype='application/json')

def getSkill(request):
    index = int(request.GET.get('index'))
    elementId = int(request.GET.get('elementId'))
    elementName = getSkillName(elementId)
    return HttpResponse(json.dumps({'code':1,'result':[index,elementName]}), mimetype='application/json')

def getSkillName(elementId):
    cursor = connection.cursor()
    sql = """ select a.deployId,b.value from mvc_skill a,mvc_language b where b.`id` = a.`name` and b.`type` = 7 and b.`key`='name' and a.`deployId`=%s """
    cursor.execute(sql,(elementId,))
    result = cursor.fetchall()
    resultName = "未选择"
    for element in result : 
        resultName = element[1]
    return resultName
    

def getNPCList(request):
    cursor = connection.cursor()
    sql = """ select a.deployId,b.value from mvc_npc a,mvc_language b where b.`id` = a.`name` and b.`type` = 4 and b.`key`='name' order by b.value """
    cursor.execute(sql)
    result = cursor.fetchall()
    resultList = []
    resultList.append([-999,"未选择"])
    for element in result : 
        resultList.append([element[0],element[1]])
    return HttpResponse(json.dumps({'code':1,'result':json.dumps(resultList)}), mimetype='application/json')

def getNPC(request):
    index = int(request.GET.get('index'))
    elementId = int(request.GET.get('elementId'))
    elementName = getNPCName(elementId)
    return HttpResponse(json.dumps({'code':1,'result':[index,elementName]}), mimetype='application/json')

def getNPCName(elementId):
    cursor = connection.cursor()
    sql = """ select a.deployId,b.value from mvc_npc a,mvc_language b where b.`id` = a.`name` and b.`type` = 4 and b.`key`='name' and a.`deployId`=%s """
    cursor.execute(sql,(elementId,))
    result = cursor.fetchall()
    resultName = "未选择"
    for element in result : 
        resultName = element[1]
    return resultName
    

def getNPCUnitList(request):
    cursor = connection.cursor()
    sql = """ select a.deployId,b.value from mvc_npc_unit a,mvc_language b where b.`id` = a.`name` and b.`type` = 5 and b.`key`='name' order by b.value """
    cursor.execute(sql)
    result = cursor.fetchall()
    resultList = []
    resultList.append([-999,"未选择"])
    for element in result : 
        resultList.append([element[0],element[1]])
    return HttpResponse(json.dumps({'code':1,'result':json.dumps(resultList)}), mimetype='application/json')

def getNPCUnit(request):
    index = int(request.GET.get('index'))
    elementId = int(request.GET.get('elementId'))
    elementName = getNPCUnitName(elementId)
    return HttpResponse(json.dumps({'code':1,'result':[index,elementName]}), mimetype='application/json')

def getNPCUnitName(elementId):
    cursor = connection.cursor()
    sql = """ select a.deployId,b.value from mvc_npc_unit a,mvc_language b where b.`id` = a.`name` and b.`type` = 5 and b.`key`='name' and a.`deployId`=%s """
    cursor.execute(sql,(elementId,))
    result = cursor.fetchall()
    resultName = "未选择"
    for element in result : 
        resultName = element[1]
    return resultName
    
